package com.jf.springmvc.requestparams;

import com.jf.springmvc.requestparams.bean.Master;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;


/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-10-31
 * Time:0:01
 */
@RequestMapping("/vote")
@Controller
public class VoteHandler {
    @GetMapping("/params")
    public String getParams(@RequestParam(name = "id", required = false) String id) {
        System.out.println("get param id = " + id);
        return "success";
    }
    @GetMapping("/header")
    public String getHeader(@RequestHeader("user-agent") String userAgent, @RequestHeader("Accept-Encoding") String acceptEncoding) {
        System.out.println("get header user-agent = " + userAgent);
        System.out.println("get header accept-encoding = " + acceptEncoding);
        return "success";
    }
    /*
    SpringMVC可以将Post提交的数据封装成Bean    Get提交的数据似乎不行
    要求:
        1.参数名与Bean的属性名相同
        2.若Bean中包含Bean2,Bean2的属性通过级联操作匹配  例pet.id
     */
    @PostMapping("/bean")
    public String getBean(Master master) {
        System.out.println("get bean master = " + master);
        return "success";
    }
    /*
    HttpServlet的API可以直接传参
    各种对象也可以直接传参,如HttpSession/Reader/Writer...,获取的对象和request.getXxx()是同一对象
     */
    @GetMapping("/servlet")
    public String servletAPI(HttpServletRequest request, HttpSession session) {
        String name = request.getParameter("name");
        System.out.println("servlet-param name = " + name);
        session.setAttribute("name", name);
        return "success";
    }
    /*
    SpringMVC会将封装的Bean以类名首字母小写放入requestScope
     */
    @PostMapping("/request")
    public String putInRequest(Master master, HttpServletRequest request) {
        request.setAttribute("address", "beijing");
        master.setName("kiki");
        return "vote_ok";
    }
    /*
    可以获得requestScope的Map,SpringMVC已将封装完Bean放入requestScope
     */
    @PostMapping("/map")
    public String putInMap(Master master, Map<String, Object> map) {
        map.put("address", "beijing");
        System.out.println(map);
        master.setId(55);
        return "vote_ok";
    }
    /*
    通过ModelAndView将数据放入requestScope
    注意:
        1.返回ModelAndView
        2.过去返回的String会被SpringMVC自动封装成ModelAndView的ViewName,交给DispatcherServlet
     */
    @PostMapping("/modelAndView")
    public ModelAndView putInModelAndView(Master master) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("address", "beijing");
        master.getPet().setId(66);
        modelAndView.setViewName("vote_ok");
        return modelAndView;
    }
    /*
    将数据放入sessionScope中
     */
    @PostMapping("/session")
    public String putInSession(Master master, HttpSession session) {
        session.setAttribute("address", "beijing");
        session.setAttribute("master", master);
        return "vote_ok";
    }
    /*
    前置方法  切片至请求方法前--表示调用当前Controller的所有请求方法前,先调用该方法
     */
    @ModelAttribute
    public void prepareModel() {
        System.out.println("prepare model");
    }
}
